package be.fomp.jeve.application.data.standard.kills;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;

import be.fomp.jeve.application.data.AbstractResultImpl;
import be.fomp.jeve.application.data.standard.Kills;
import be.fomp.jeve.core.exceptions.JEveException;

public final class KillsImpl extends AbstractResultImpl implements Kills{
	static final long serialVersionUID = 1L;

	private Map<Integer, KillStats> kills = new HashMap<Integer, KillStats>();
	
	@SuppressWarnings("unchecked")
	public KillsImpl(Document xmlDocument) throws JEveException{
		super(xmlDocument);
		
		Element root = xmlDocument.getRootElement();
		
		List<Element> resultSet = root.getChild("result").getChild("rowset").getChildren();
		for(Element killStatsElement: resultSet){
			Integer solarSystemId = Integer.valueOf(killStatsElement.getAttributeValue("solarSystemID"));
			KillStats killStats = new KillStatsImpl(killStatsElement);
			this.kills.put(solarSystemId, killStats);
		}
	}
	
	public Map<Integer, KillStats> getKillStats()
	{
		return this.kills;
	}
	
	public KillStats getKillStatsFromSolarSystem(int solarSystemId)	{
		return this.kills.get(solarSystemId);
	}
	
	public void writeObject(ObjectOutputStream os) {
		try {
			os.defaultWriteObject();
			os.writeObject(this.kills);
		} catch (Exception e) { e.printStackTrace();  }
	}

	@SuppressWarnings("unchecked")
	public void readObject (ObjectInputStream is) {
		try {
			is.defaultReadObject();
			this.kills = ((Map<Integer, KillStats>)is.readObject());
		} catch (Exception e) {e.printStackTrace();}
	}
}
